<?php

/**
 * Specify question object. Questions table.
 */
class question
{
    /**
     * int question id
     */
    var $id;

    /**
     * int test id
     */
    var $test;

    /**
     * string question text
     */
    var $text;

    /**
     * int question number
     */
    var $number;

    /**
     * string question time
     */
    var $time;

    /**
     * int question type
     */
    var $type;

    /**
     * string question explanation
     */
    var $explanation;

    /**
     * binary question image
     */
    var $image;

    /**
     * string question reference
     */
    var $reference;

    /**
     * string question etalon
     */
    var $etalon;

    /**
     * int etalon score
     */
    var $etalon_score;

    /**
     * int (0, 1) case sensetive
     */
    var $is_case_sensetive;

    /**
     * int theme id
     */
    var $theme_id;

    /**
     * string question text html
     */
    var $text_html;

    /**
     * binary question text
     */
    var $text_bin;

    /**
     * string question picture url
     */
    var $picture_url;

    /**
     * int question weight
     */
    var $weight;

    /**
     * int question hint id
     */
    var $hint;

    /**
     * string question css
     */
    var $question;
}

/**
 * Add question.
 *
 * @param $p_question question class
 *
 * @return RecordSet or false
 */
function add_question($p_question)
{
    $t_question_text = db_prepare_string($p_question->text);
    $t_question_number = db_prepare_int($p_question->number);
    $t_question_time = db_prepare_string($p_question->time);
    $t_question_type = db_prepare_int($p_question->type);
    $t_question_explanation =
            db_prepare_string($p_question->explanation);

    $t_question_image = ($p_question->image == NULL) ? 'NULL' : db_prepare_string($p_question->image);
    $t_question_reference = db_prepare_string($p_question->reference);
    $t_question_etalon = db_prepare_string($p_question->etalon);
    $t_question_etalon_score =
            db_prepare_string($p_question->etalon_score);
    $t_question_is_case_sensetive =
            db_prepare_int($p_question->is_case_sensetive);
    $t_question_theme_id = db_prepare_int($p_question->theme_id);
    $t_question_text_html = db_prepare_string($p_question->text_html);
    $t_question_text_bin = ($p_question->text_bin == NULL) ? 'NULL' : db_prepare_string($p_question->text_bin);
    $t_question_picture_url =
            db_prepare_string($p_question->picture_url);
    $t_question_weight = db_prepare_int($p_question->weight);
    $t_question_hint = db_prepare_int($p_question->hint);
    $t_question_css = db_prepare_string($p_question->css);

    $query =  'INSERT INTO '.DB_TABLE_QUESTIONS.'(
            `question_text`,
            `question_number`,
            `question_time`,
            `question_type`,
            `question_explanation`,
            `question_image`,
            `question_reference`,
            `question_etalon`,
            `question_etalon_score`,
            `question_is_case_sensetive`,
            `question_theme_id`,
            `question_text_html`,
            `question_text_bin`,
            `question_picture_url`,
            `question_weight`,
            `question_hint`,
            `question_css`)
         VALUES(
            '.$t_question_text.',
            '.$t_question_number.',
            '.$t_question_time.',
            '.$t_question_type.',
            '.$t_question_explanation.',
            '.$t_question_image.',
            '.$t_question_reference.',
            '.$t_question_etalon.',
            '.$t_question_etalon_score.',
            '.$t_question_is_case_sensetive.',
            '.$t_question_theme_id.',
            '.$t_question_text_html.',
            '.$t_question_text_bin.',
            '.$t_question_picture_url.',
            '.$t_question_weight.',
            '.$t_question_hint.',
            '.$t_question_css.'
            )';

    return db_exec($query);
}

/**
 * Get questions.
 *
 * @param $p_id int test id
 *
 * @return array string questions
 */
function get_questions($p_id, $p_page = 1, $p_count = 0)
{
    $t_id = db_prepare_int($p_id);
    $t_page = db_prepare_int($p_page);
    $t_count = db_prepare_int($p_count);

    if ($p_count != 0)
    {
        $limit = ($t_page-1)*$t_count;
        $limit_str = "$limit, $t_count";
    }
    else
    {
        $limit_str = '';
    }

    $query =  'SELECT '.DB_TABLE_QUESTIONS.'.`question_text_html`
           FROM  '.DB_TABLE_TESTS.', '.
               DB_TABLE_QUESTIONS.', '.
               DB_TABLE_THEMES.
          ' WHERE '.DB_TABLE_THEMES.'.`theme_test_id` = '.DB_TABLE_TESTS.'.`id`
          AND   '.DB_TABLE_QUESTIONS.'.`question_theme_id` = '.DB_TABLE_THEMES.'.`theme_id`
          AND   '.DB_TABLE_TESTS.'.`id` = '.$t_id;
    if ($limit_str != '')
    {
        $query .= ' LIMIT '. $limit_str;
    }

    return db_query($query);
}

/**
 * Get questions count for test id.
 *
 * @param $p_id int test id
 *
 * @return int questions count
 */
function get_questions_count($p_id)
{
    $t_id = db_prepare_int($p_id);
    $query = 'SELECT COUNT('.DB_TABLE_QUESTIONS.'.`question_text_html`) AS `_count_`
           FROM  '.DB_TABLE_TESTS.', '.
               DB_TABLE_QUESTIONS.', '.
               DB_TABLE_THEMES.
          ' WHERE '.DB_TABLE_THEMES.'.`theme_test_id` = '.DB_TABLE_TESTS.'.`id`
          AND   '.DB_TABLE_QUESTIONS.'.`question_theme_id` = '.DB_TABLE_THEMES.'.`theme_id`
          AND   '.DB_TABLE_TESTS.'.`id` = '.$t_id;

    $result = db_query($query);

    return isset($result[0]['_count_']) ? $result[0]['_count_'] : 0;
}

/**
 * Get questions array for test id.
 *
 * @param $p_id int test id
 *
 * @return array questions array
 */
function get_questions_for_test_id($p_id)
{
    $t_id = db_prepare_int($p_id);

    $query =  'SELECT '.DB_TABLE_QUESTIONS.'.*
           FROM  '.DB_TABLE_TESTS.', '.
               DB_TABLE_QUESTIONS.', '.
               DB_TABLE_THEMES.
          ' WHERE '.DB_TABLE_THEMES.'.`theme_test_id` = '.DB_TABLE_TESTS.'.`id`
          AND   '.DB_TABLE_QUESTIONS.'.`question_theme_id` = '.DB_TABLE_THEMES.'.`theme_id`
          AND   '.DB_TABLE_TESTS.'.`id` = '.$t_id;

    return db_query($query);
}

/**
 * Get question.
 *
 * @param $p_question_id int question id
 *
 * @return question class
 */
function get_question($p_question_id)
{
    $t_question_id = db_prepare_int($p_question_id);

     $query = 'SELECT *
            FROM '.DB_TABLE_QUESTIONS.
            ' WHERE `question_id` = '.$t_question_id;
    $result = db_query($query);

    $question = new question();

    if (isset($result[0]))
    {
        $question->id = $result[0]['question_id'];
        $question->etalon = $result[0]['question_etalon'];
        $question->etalon_score = $result[0]['question_etalon_score'];
        $question->explanation = $result[0]['question_explanation'];
        $question->hint = $result[0]['question_hint'];
        $question->image = $result[0]['question_image'];
        $question->is_case_sensetive = $result[0]['question_is_case_sensetive'];
        $question->number = $result[0]['question_number'];
        $question->picture_url = $result[0]['question_picture_url'];
        $question->reference = $result[0]['question_reference'];
        $question->text = $result[0]['question_text'];
        $question->text_bin = $result[0]['question_text_bin'];
        $question->text_html = $result[0]['question_text_html'];
        $question->theme_id = $result[0]['question_theme_id'];
        $question->time = $result[0]['question_time'];
        $question->type = $result[0]['question_type'];
        $question->weight = $result[0]['question_weight'];
        $question->picture_url = $result[0]['question_picture_url'];
        $question->css = $result[0]['question_css'];
    }

    return $question;
}

/**
 * Get questions for theme id.
 *
 * @param $p_theme_id int test id
 *
 * @return array question class
 */
function get_questions_for_theme_id($p_theme_id)
{
    $t_theme_id = db_prepare_int($p_theme_id);

    $query = 'SELECT * FROM '.DB_TABLE_QUESTIONS.
        ' WHERE `question_theme_id` = '.$t_theme_id.
        ' ORDER BY `question_number` ASC';

    return db_query($query);
}

/**
 * Get questions count for theme id.
 *
 * @param $p_id int theme id
 *
 * @return int questions count
 */
function get_questions_count_for_theme_id($p_id)
{
    $t_id = db_prepare_int($p_id);

    return db_count(DB_TABLE_QUESTIONS, '`question_theme_id` = '.$t_id);
}

/**
 * Delete questions for theme id.
 *
 * @param $p_theme_id int theme id
 *
 * @return RecordSet or false
 */
function delete_questions_for_theme_id($p_theme_id)
{
    $t_theme_id = db_prepare_int($p_theme_id);
    $questions = get_questions_for_theme_id($t_theme_id);

    $id_array = array();
    foreach($questions as $question)
    {
        delete_answers_for_question_id($question['question_id']);
        $id_array[] = $question['question_id'];
    }

    $tmp = "";
    $array = array_values($id_array);
    $size = sizeof($array);

    if ($size == 0)
        return FALSE;

    for($i=0; $i<$size-1; $i++)
        $tmp .=  db_prepare_int($id_array[$i]).", ";
    $tmp .= db_prepare_int($array[$size-1]);

    $query = 'DELETE FROM '.DB_TABLE_QUESTIONS.
        ' WHERE `question_id` IN ('.$tmp.')';

    return db_exec($query);
}

